// SnapIn.h : Declaration of the CSnapIn

#pragma once
#include "resource.h"       // main symbols
#include "atlbase.h"
#include "MMC.h"
#include "atlsnap.h"
#include "UserComponent.h"
#include "MMC_2_0_i.h"
#include "Server.h"
#include "ActiveX.h"
//#include "ScopeItem.h"
//CComModule _Module;
class CConnectionActiveX;


class CData : public CBaseNodeItem<CData>
{
	CServer *m_pServer;
	//CMsgViewNode* m_pMsgNode;
	CConnectionActiveX *m_pConAxNode;
public:

	CData()
	{
		m_pServer = new CServer;
		//m_pMsgNode  = new CMsgViewNode;

		m_pConAxNode = new CConnectionActiveX;
		// Image indexes may need to be modified depending on the images specific to 
		// the snapin.
		memset(&m_scopeDataItem, 0, sizeof(SCOPEDATAITEM));
		m_scopeDataItem.mask = SDI_STR | SDI_IMAGE | SDI_OPENIMAGE | SDI_PARAM | SDI_CHILDREN;
		m_scopeDataItem.displayname = MMC_CALLBACK;
		m_scopeDataItem.nImage = 0; 		// May need modification
		m_scopeDataItem.nOpenImage = 0; 	// May need modification
		m_scopeDataItem.lParam = (LPARAM) this;
		m_scopeDataItem.cChildren = 2;

		memset(&m_resultDataItem, 0, sizeof(RESULTDATAITEM));
	}

	~CData()
	{
		delete m_pServer;
		//delete m_pMsgNode;
		delete m_pConAxNode;
	}
//	STDMETHOD(CreatePropertyPages)(LPPROPERTYSHEETCALLBACK lpProvider,long handle, IUnknown* pUnk,
//							DATA_OBJECT_TYPES type)
//	{
//		if (type == CCT_SNAPIN_MANAGER)
//		{
////			CChooseCompPage* pPage = new CChooseCompPage(handle, true, _T("SnapIn"));
////			HRESULT hr = lpProvider->AddPage(pPage->Create());
//			// The second parameter  to the property page class constructor
//			// should be true for only one page.
//			return S_OK;
//		}
//		return E_UNEXPECTED;
//	}

	STDMETHOD(QueryPagesFor)(DATA_OBJECT_TYPES type)
	{
		if (type == CCT_SNAPIN_MANAGER)
			return S_OK;

		return S_FALSE;
	}

	HRESULT OnShow(IConsole *spConsole, BOOL bShow)
	{
		HRESULT hr = S_FALSE;
		if(bShow)
		{        
			CComQIPtr<IResultData, &IID_IResultData> spResultData(spConsole);
			CComQIPtr<IHeaderCtrl, &IID_IHeaderCtrl> spHeaderCtrl(spConsole);
			HRESULT hr = spResultData->DeleteAllRsltItems();
			hr = spHeaderCtrl->InsertColumn( 0, L"Column - 1", 0,  MMCLV_AUTO);
			hr = spHeaderCtrl->InsertColumn( 1, L"Column - 2", 0, MMCLV_AUTO);
		}	 
		return hr;
	}
	HRESULT OnExpand(BOOL bExpand, IConsole *pConsole, HSCOPEITEM parent);
};





class CUserComponent;

//define CSnapIn. Done
class CSnapIn : public CComObjectRootEx<CComSingleThreadModel>,
public CSnapInObjectRoot<1, CSnapIn>,
	public IComponentDataImpl<CSnapIn, CUserComponent>,
	public IExtendPropertySheetImpl<CSnapIn>,
	public IExtendContextMenuImpl<CSnapIn>,
	public IPersistStreamInit,
	public CComCoClass<CSnapIn, &CLSID_SnapIn>
	//ISnapinHelp2
{
public:
	CSnapIn()
	{
		//m_pNode = new CServer;
		m_pNode = new CData;
		_ASSERTE(m_pNode != NULL);
		m_pComponentData = this;
	}

	~CSnapIn()
	{
		delete m_pNode;
		m_pNode = NULL;
	}

BEGIN_COM_MAP(CSnapIn)
	COM_INTERFACE_ENTRY(IComponentData)
    COM_INTERFACE_ENTRY(IExtendPropertySheet)
    COM_INTERFACE_ENTRY(IExtendContextMenu)
	COM_INTERFACE_ENTRY(IPersistStreamInit)
	//COM_INTERFACE_ENTRY(ISnapinHelp2)
	//COM_INTERFACE_ENTRY(ISnapinHelp)
END_COM_MAP()

DECLARE_REGISTRY_RESOURCEID(IDR_SNAPIN)

DECLARE_NOT_AGGREGATABLE(CSnapIn)

/////ISnapinHelp2
	 HRESULT STDMETHODCALLTYPE GetLinkedTopics(/* [out] */ LPOLESTR __RPC_FAR *lpCompiledHelpFiles)
	 {
		 return S_FALSE;
	 }
/////ISnapinHelp
     HRESULT STDMETHODCALLTYPE GetHelpTopic(/* [out] */ LPOLESTR __RPC_FAR *lpCompiledHelpFile);

	STDMETHOD(GetClassID)(CLSID *pClassID)
	{
		*pClassID = CLSID_SnapIn;
		return S_OK;
	}	

	STDMETHOD(IsDirty)()
	{
		return S_FALSE; //implement the dirty mechanism if needed
	}

	STDMETHOD(Load)(IStream *pStm)
	{
		char buff[MAX_PATH];
		return pStm->Read(buff, sizeof(buff), NULL); //change to something usefull
	}

	STDMETHOD(Save)(IStream *pStm, BOOL fClearDirty)
	{
		char buff[MAX_PATH];
		sprintf(buff,"local");
		return pStm->Write(buff, sizeof(buff), NULL);//change to something usefull
	}

	STDMETHOD(GetSizeMax)(ULARGE_INTEGER *pcbSize)
	{
		pcbSize->QuadPart = MAX_PATH; //change to something usefull
		return S_OK;
	}

	STDMETHOD(InitNew)()
	{
		return S_OK;
	}

	STDMETHOD(Initialize)(LPUNKNOWN pUnknown);

	static void WINAPI ObjectMain(bool bStarting)
	{
		if (bStarting)
			CSnapInItem::Init();
	}
	static HRESULT LoadImages(IImageList*);

	STDMETHOD(Notify)(LPDATAOBJECT lpDataObject,MMC_NOTIFY_TYPE event,long arg,long param)
	{
		HRESULT hr = S_FALSE;
		if(lpDataObject != NULL)
		{
			hr= IComponentDataImpl<CSnapIn, CUserComponent>::Notify(lpDataObject,event,arg,param);
		}
		else
		{
			if(event == MMCN_PROPERTY_CHANGE)
			{
				if(arg) //scope
				{

				}
				else //result
				{

				}
			}
		}
		return hr;
	}
};
OBJECT_ENTRY_AUTO(__uuidof(SnapIn), CSnapIn)
//end define CSnapIn. Done